﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;

namespace TugasAkhir.Helpers
{
    public static class SqlServer
    {
        private const string ConnectionString = @"Data Source=PANICCODE\REZKIWULAN;Initial Catalog=TA;Integrated Security=True";

        public static bool ExecuteNonQuery(string query)
        {
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand(query, connection);
                command.Connection.Open();
                try
                {
                    command.ExecuteNonQuery();
                    return true;
                }
                catch (SqlException sqlException)
                {
                    Debug.WriteLine(sqlException.Message);
                    Debug.WriteLine(sqlException.Number);
                    return false;
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    return false;
                }
            }
        }

        public static bool ExecuteReader(string query, out DataSet dataSet)
        {
            dataSet = new DataSet();
            using (SqlConnection connection = new SqlConnection(ConnectionString))
            {
                SqlCommand command = new SqlCommand(query, connection);
                command.Connection.Open();
                SqlDataAdapter adapter = new SqlDataAdapter(command);
                try
                {
                    adapter.Fill(dataSet);
                    return true;
                }
                catch (SqlException sqlException)
                {
                    Debug.WriteLine(sqlException.Message);
                    Debug.WriteLine(sqlException.Number);
                    return false;
                }
                catch (Exception exception)
                {
                    Debug.WriteLine(exception.Message);
                    return false;
                }
            }
        }
    }
}